package com.hemerasolutions.v2trainer.service;

import java.util.List;

import org.apache.commons.lang.mutable.MutableInt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hemerasolutions.v2trainer.dao.ExceptionDao;
import com.hemerasolutions.v2trainer.dao.interfaces.PhotoDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.RemoteVideoDaoInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.VideoDaoInterface;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.multimedia.RemoteVideo;
import com.hemerasolutions.v2trainer.model.multimedia.Video;
import com.hemerasolutions.v2trainer.service.interfaces.MultimediaServiceInterface;

@Service("multimediaService")
public class MultimediaService extends BaseService implements MultimediaServiceInterface   {

	private static final long serialVersionUID = 1L;

	@Autowired
	private VideoDaoInterface videoDao;

	@Autowired
	private PhotoDaoInterface photoDao;
	
	@Autowired
	private RemoteVideoDaoInterface remoteVideoDao;
	
	
	@Override
	public RemoteVideo uploadToYouTube(RemoteVideo selRemoteVideo, MutableInt videoUploadProgress)
			throws ExceptionService {

		try{
			return remoteVideoDao.persistInRemoteStorage(selRemoteVideo, videoUploadProgress);
		}catch(ExceptionDao e){
			throw new ExceptionService(e.getMessage());
		}
	}

	@Override
	public boolean testConnection() throws ExceptionService  {

		try{
			remoteVideoDao.listVideosInYouTube();
			return true;
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
	}

	@Override
	public List<com.google.api.services.youtube.model.Video> listYouTubeVideos() throws ExceptionService {
		try{
			return remoteVideoDao.listVideosInYouTube();
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
	}

	@Override
	public List<Video> listVideos() throws ExceptionService {
		try{
			return videoDao.list();
		}catch(ExceptionDao ex){
			throw new ExceptionDao(ex.getMessage());
		}
	}

	@Override
	public boolean isEmptyDBVideos() {

		if(this.videoDao.list().isEmpty()){
			return true;
		}
		
		return false;
	}


	@Override
	public RemoteVideo readDefaultExerciseRemoteVideo() {
		return this.remoteVideoDao.getByUuid(RemoteVideo.DEFAULT_EXERCISE_VIDEO_UUID);
		
	}

	@Override
	public RemoteVideo readDefaultEquipmentRemoteVideo() {
		return this.remoteVideoDao.getByUuid(RemoteVideo.DEFAULT_EQUIPMENT_VIDEO_UUID);
	}

	@Override
	public Video copyDefaultEquipmentVideo() {
		Video video = new Video();
		video.setRemoteVideo(this.readDefaultEquipmentRemoteVideo());
		return video;
	}

	@Override
	public Video copyDefaultExerciseVideo() {
		Video video = new Video();
		video.setRemoteVideo(this.readDefaultExerciseRemoteVideo());
		return video;
	}
		
	
	
	@Override
	public void saveDbVideo(Video video) throws ExceptionService {
		try{
			this.videoDao.save(video);

		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
		
	}

	@Override
	public void deleteDefaultEquipmentVideo() throws ExceptionService {
		try{
			RemoteVideo rm = this.readDefaultEquipmentRemoteVideo(); 

			remoteVideoDao.deleteYouTubeVideo(rm);
			rm.setStorageId(null);
			rm.setVideoHascode(null);
			remoteVideoDao.save(rm);
			
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
	}

	
	@Override
	public void deleteDefaultExerciseVideo() throws ExceptionService {
		try{
			RemoteVideo rm = this.readDefaultExerciseRemoteVideo(); 

			remoteVideoDao.deleteYouTubeVideo(rm);
			rm.setStorageId(null);
			rm.setVideoHascode(null);
			remoteVideoDao.save(rm);
			
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
	}

	@Override
	public void deleteVideo(Video video) throws ExceptionService{
		try{

			videoDao.delete(video);

			if(	video.getRemoteVideo().getUuid() != RemoteVideo.DEFAULT_EQUIPMENT_VIDEO_UUID &&
				video.getRemoteVideo().getUuid() != RemoteVideo.DEFAULT_EXERCISE_VIDEO_UUID){
				
				remoteVideoDao.deleteYouTubeVideo(video.getRemoteVideo());
				remoteVideoDao.delete(video.getRemoteVideo());
			}
			
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex.getMessage());
		}
	}

	
	
	@Override
	public Photo readDefaultExercisePhoto() throws ExceptionService {
		Photo photo, photo2;
		photo = photoDao.findByLocation("default_exercise.jpg"); 

		photo2 = new Photo(photo);
		
		return photo2;
	}

	@Override
	public Photo readDefaultEquipmentPhoto() throws ExceptionService {
		Photo photo, photo2;
		photo = photoDao.findByLocation("default_equipment.jpg"); 

		photo2 = new Photo(photo);
		
		return photo2;
	}

	@Override
	public void deleteRemoteVideo(RemoteVideo rm) throws ExceptionService{
		try{
			remoteVideoDao.delete(rm);
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex);
		}
		
	}
	
	@Override
	public RemoteVideo saveRemoteVideo(RemoteVideo rm) throws ExceptionService{
		try{
			return remoteVideoDao.save(rm);
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex);
		}
	}

	@Override
	public RemoteVideo createRemoteVideo(RemoteVideo rm) throws ExceptionService{
		try{
			return remoteVideoDao.create(rm);
		}catch(ExceptionDao ex){
			throw new ExceptionService(ex);
		}
	}

	@Override
	public void disconnectYouTube() throws ExceptionService {
		try {
			this.remoteVideoDao.disconnect();
		} catch (ExceptionDao e) {
			throw e;
		}
	}

}
